home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "win32.h"
-
- #define _MAX_PATH 260 /* max. length of full pathname */
- #define _MAX_DRIVE 3 /* max. length of drive component */
- #define _MAX_DIR 256 /* max. length of path component */
- #define _MAX_FNAME 256 /* max. length of file name component */
- #define _MAX_EXT 256 /* max. length of extension component */
- #define __min(a,b) (((a) < (b)) ? (a) : (b))
-
- void parse (char *path, char *drive, char *dir, char *fname, char *ext);
-
- int main(int argc, char **argv)
- {
-
- /*char path[_MAX_PATH];*/
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
-
- if (argc <= 1)
- {
- printf("parse_filename usage:\n");
- printf("parse_filename path_name_to_parse\n");
- printf("The output will be placed in the environment variables:\n");
- printf("DRIVE, DIR, FNAME, and EXT\n");
- return 0;
- }
-
- /* now parse the FULL_PATH into components */
- parse (argv[1], drive, dir, fname, ext);
-
- /* set each component in the environment */
- SetEnvironmentVariable("drive", drive);
- SetEnvironmentVariable("dir", dir);
- SetEnvironmentVariable("fname", fname);
- SetEnvironmentVariable("ext", ext);
-
- return 0;
- }
-
-
- void parse (char *path, char *drive, char *dir, char *fname, char *ext)
- {
- char *p;
- char *last_slash = NULL, *dot = NULL;
- unsigned len;
-
- /* we assume that the path argument has the following form:
- * <drive><dir><fname><ext>
- */
-
- /* extract drive letter and :, if any */
-
- if ((strlen(path) >= (_MAX_DRIVE - 2)) && (*(path + _MAX_DRIVE - 2) == ':')) {
- if (drive) {
- strncpy(drive, path, _MAX_DRIVE - 1);
- *(drive + _MAX_DRIVE-1) = '\0';
- }
- path += _MAX_DRIVE - 1;
- }
- else if (drive) {
- *drive = '\0';
- }
-
- /* extract path string, if any. Path now points to the first character
- * of the path, if any, or the filename or extension, if no path was
- * specified. Scan ahead for the last occurence, if any, of a '/' or
- * '\' path separator character. If none is found, there is no path.
- * We will also note the last '.' character found, if any, to aid in
- * handling the extension.
- */
-
- for (last_slash = NULL, p = (char *)path; *p; p++) {
- if (*p == '/' || *p == '\\')
- /* point to one beyond for later copy */
- last_slash = p + 1;
- else if (*p == '.')
- dot = p;
- }
-
- if (last_slash) {
-
- /* found a path - copy up through last_slash or max. characters
- * allowed, whichever is smaller
- */
-
- if (dir) {
- len = __min(((char *)last_slash - (char *)path) / sizeof(char),
- (_MAX_DIR - 1));
- strncpy(dir, path, len);
- *(dir + len) = '\0';
- }
- path = last_slash;
- }
- else if (dir) {
-
- /* no path found */
-
- *dir = '\0';
- }
-
- /* extract file name and extension, if any. Path now points to the
- * first character of the file name, if any, or the extension if no
- * file name was given. Dot points to the '.' beginning the extension,
- * if any.
- */
-
- if (dot && (dot >= path)) {
- /* found the marker for an extension - copy the file name up to
- * the '.'.
- */
- if (fname) {
- len = __min(((char *)dot - (char *)path) / sizeof(char),
- (_MAX_FNAME - 1));
- strncpy(fname, path, len);
- *(fname + len) = '\0';
- }
- /* now we can get the extension - remember that p still points
- * to the terminating nul character of path.
- */
- if (ext) {
- len = __min(((char *)p - (char *)dot) / sizeof(char),
- (_MAX_EXT - 1));
- strncpy(ext, dot, len);
- *(ext + len) = '\0';
- }
- }
- else {
- /* found no extension, give empty extension and copy rest of
- * string into fname.
- */
- if (fname) {
- len = __min(((char *)p - (char *)path) / sizeof(char),
- (_MAX_FNAME - 1));
- strncpy(fname, path, len);
- *(fname + len) = '\0';
- }
- if (ext) {
- *ext = '\0';
- }
- }
- }
-